package com.cxs.controller;

import com.cxs.dto.RoleDTO;
import com.cxs.dto.RoleSearchDTO;
import com.cxs.enums.HospitalErrorResultEnum;
import com.cxs.exception.HospitalException;
import com.cxs.pojo.Role;
import com.cxs.service.RoleService;
import com.cxs.vo.MenuTreeVO;
import com.cxs.vo.Result;
import com.cxs.vo.RoleVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/10/18  17:52
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 加载所有的角色信息 + 搜索
     *
     * @param searchDTO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/loadAllRole")
    public Result loadAllRole(RoleSearchDTO searchDTO,
                              @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        Role role = new Role();
        BeanUtils.copyProperties(searchDTO, role);
        // 查询数据
        PageInfo<Role> pageInfo = roleService.selectAllCondition(role, pageNum, pageSize);
        // 封装vo数据
        List<Role> list = pageInfo.getList();
        List<RoleVO> voList = new ArrayList<>();
        for (Role obj : list) {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(obj, roleVO);
            voList.add(roleVO);
        }
        Result result = new Result();
        result.put("count", pageInfo.getTotal());
        result.put("code", 0);
        result.put("msg", "");
        result.put("data", voList);
        return result;
    }

    /**
     * 更新角色
     *
     * @param roleDTO
     * @return
     */
    @PostMapping("/updateRole")
    public Result updateRole(RoleDTO roleDTO) {
        if (StringUtils.isEmpty(roleDTO.getRoleId()) || roleDTO.getRoleId() <= 0) {
            log.error("【角色模块】:角色修改失败，roleId = {}", roleDTO.getRoleId());
            throw new HospitalException(HospitalErrorResultEnum.ROLE_PARAMETER_ERROR);
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO, role);
        roleService.update(role);

        return Result.Ok("角色信息修改成功");
    }

    /**
     * 添加角色
     *
     * @param roleDTO
     * @return
     */
    @PostMapping("/insertRole")
    public Result insertRole(RoleDTO roleDTO) {

        Role role = new Role();
        BeanUtils.copyProperties(roleDTO, role);
        roleService.insert(role);

        return Result.Ok("角色添加成功");
    }

    /**
     * 删除角色信息
     *
     * @param roleId
     * @return
     */
    @GetMapping("/deleteRole/{id}")
    public Result deleteRole(@PathVariable("id") Integer roleId) {
        if (StringUtils.isEmpty(roleId) || roleId <= 0) {
            log.error("【角色模块】:角色删除失败，roleId = {}", roleId);
            throw new HospitalException(HospitalErrorResultEnum.ROLE_PARAMETER_ERROR);
        }
        roleService.delete(roleId);

        return Result.Ok("角色删除成功");
    }

    /**
     * 加载角色的菜单列表
     *
     * @param roleId
     * @return
     */
    @PostMapping("/initRoleMenuTreeJson")
    public Result initRoleMenuTreeJson(Integer roleId) {
        if (StringUtils.isEmpty(roleId) || roleId <= 0) {
            log.error("【角色模块】:角色权限获取失败，roleId = {}", roleId);
            throw new HospitalException(HospitalErrorResultEnum.ROLE_PARAMETER_ERROR);
        }

        List<MenuTreeVO> treeVOS = roleService.loadRoleMenu(roleId);
        Result result = new Result();
        result.put("code", 0);
        result.put("msg", "");
        result.put("data", treeVOS);
        return result;
    }

    /**
     * 给角色授权
     *
     * @return
     */
    @PostMapping("/saveRoleMenu")
    public Result saveRoleMenu(Integer roleId, Integer[] ids) {
        if (StringUtils.isEmpty(roleId) || roleId <= 0) {
            log.error("【角色模块】:角色授权获取失败，roleId = {}", roleId);
            throw new HospitalException(HospitalErrorResultEnum.ROLE_PARAMETER_ERROR);
        }

        roleService.saveRoleMenu(roleId, ids);

        return Result.Ok("授权成功");
    }
}
